<?php
namespace app\admin\controller;
use framework\ext\Check;

class GroupController extends \app\base\controller\AdminController{

	//用户列表
	public function index(){
        $act = form('get.act');
        if($act == 'del'){
            $fid = form('get.fid',0,intval);
            if($fid == 1){
                $this->jsonMsg('系统超级权限组禁止删除!');
            } 
            $result =  model('auth')->_delete($fid);
            if($result){
                $this->jsonMsg('删除成功',1);
            }else{
                $this->jsonMsg('删除失败,已有管理员设置本权限组,请先修改管理员权限。');
            } 
        }else{
            $page = form('get.page',0,intval);
            $this->rel = model('auth')->pager($page,20)->_select(array(0=>'id <> 1'));
            $this->pager = getPage(model('auth')->pager);
            $this->display();
        }
	}

    //添加或编辑用户
    public function edit(){
        if($this->isPost()){
            $fid = form('post.fid',0,intval);
            $name = form('post.name');
            $msg = Check::rule(array(Check::must($name),'请输入用户名'));
            if(true !== $msg){
                $this->jsonMsg($msg);
            }
            $fid = form('post.fid',0,intval);
            $data['name'] = $name;
            $data['update'] = time();
            if(is_array($_POST[auth])){
                $data['auth_value'] = json_encode($_POST[auth]);
            }
            if(is_array($_POST[menu])){
                $data['app_nav'] = json_encode($_POST[menu]);
            }
            if($fid){
                if($fid == 1){
                    $this->jsonMsg('提交失败,系统超级权限组禁止操作!');
                } 
                $condition['id']= $fid;
                $rel = model('auth')->_update($data,$condition);
            }else{    
                $rel = model('auth')->_update($data);
            }
            if($rel){
                $this->jsonMsg('提交成功',1,url('group/index'));
            }else{
                $this->jsonMsg('提交失败,请返回重试');
            }
        }else{
            $this->fid = form('get.fid',0,intval);
            $condition['id'] = $this->fid;
            $this->info = model('auth')->_find($condition);
            $this->list = model('nav')->nav_lists();
            if($this->info['auth_value']){
                $this->auth = json_decode($this->info['auth_value']);
            }else{
                $this->auth = array();
            }
            if($this->info['app_nav']){
                $this->menu = json_decode($this->info['app_nav']);
            }else{
                $this->menu = array();
            }
            $this->display();
        }
    }
}